/*
* mcu2_motor_ctrl.c
*
* Created: 2012/8/4 15:10:30
*  Author: albert
*/


#ifndef F_CPU
#define F_CPU 14745600UL
#endif

#include <avr/io.h>
#include <util/delay.h>
#include "atmega128_usart.h"
#include "Timer.h"
#include "motor.h"
#include "led.h"
#include "spi.h"

int main(void)
{
	_delay_ms(500);
	USART_INIT(motor_usart_num, usart_mode,38400);
	SPI_SlaveInit();
	Timer3_200ms();
	if (usart_mode == 1)
	sei();
	timer_enable = 1; //used in usart
	led_toggle(LED_ALL);
	while(1)
	{
		current_motor_mode=SPI_SlaveReceive();
		//led_toggle(current_motor_mode);
		if(current_motor_mode==pre_motor_mode)
		{
	        spi_counter++;
		}
		else {
			pre_motor_mode=current_motor_mode;
			spi_counter++;
			switch (current_motor_mode)
			{
				case forward:
				go_forward();
				break;
				case reverse:
				go_reverse();
				break;
				case left:
				turn_left();
				break;
				case right:
				turn_right();
				break;	
			}
		}
	}
	
}